package app.base.systemmanage.systemusersrolesmanage;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import app.base.common.Utility;

import app.base.systemmanage.systemusersrolesmanage.dao.SysUsersRolesMappingDAO;

public class SystemUsersRolesFlatQuery extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
	    request.setCharacterEncoding(Utility.CHARSET_ENCODING_UTF8);
	    response.setContentType(Utility.DEFAULT_CONTENT_TYPE);

		PrintWriter out = response.getWriter(); 
		
		int start = Integer.parseInt(URLDecoder.decode(request.getParameter("start"), Utility.CHARSET_ENCODING_UTF8));
	    int limit = Integer.parseInt(URLDecoder.decode(request.getParameter("limit"), Utility.CHARSET_ENCODING_UTF8));
	    
	    String userId = request.getParameter("userId");
	    String roleId = request.getParameter("roleId");
	    
	    roleId = (roleId == null || roleId.trim().length() == 0) ? "%" : URLDecoder.decode(roleId, Utility.CHARSET_ENCODING_UTF8);
	    userId = (userId == null || userId.trim().length() == 0) ? "%" : URLDecoder.decode(userId, Utility.CHARSET_ENCODING_UTF8);
	    	    
	    Map<String, Object>  map = new HashMap<String, Object>();
	    
		map.put("start", BigDecimal.valueOf(start + 1));
		map.put("end", BigDecimal.valueOf(start + limit));
		map.put("roleId", roleId);
		map.put("userId", userId);
		
	    out.println(getGridListData(map).toString());
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
		doGet(request, response);
	}
	
	//return JSON format.
	@SuppressWarnings("unchecked")
	public StringBuffer getGridListData(Map<String, Object>  map) 
		throws UnsupportedEncodingException
	{
		ApplicationContext context = new ClassPathXmlApplicationContext(Utility.DEFAULT_SPRINGFRAMEWORK_APPLICATION_CONTEXT_FILE_PATH);
		SysUsersRolesMappingDAO sysUsersRolesMappingDAO = (SysUsersRolesMappingDAO)context.getBean(Utility.SYSTEM_USERS_ROLES_MAPPING_MANAGE_MODULE_BEAN_NAME);
				
		StringBuffer json = new StringBuffer("{totalProperty:" + sysUsersRolesMappingDAO.count_0001(map) + ",root:[");
		
		List list = sysUsersRolesMappingDAO.select_0001(map);	
		
		for(int index = 0; index < list.size(); index++)
		{
			Map resultMap = (Map) list.get(index);
			
            json.append("{" 
            	+ "userRoleId:'" + resultMap.get("USER_ROLE_ID").toString().trim()
              	+ "',userId:'" + resultMap.get("USER_ID").toString().trim()
            	+ "',roleId:'" + resultMap.get("ROLE_ID").toString().trim()
             	+ "',loginId:'" + resultMap.get("LOGIN_ID").toString().trim()
             	+ "',roleName:'" + resultMap.get("ROLE_NAME").toString().trim()
            	+ "'}");
            
            if (index < list.size() - 1) {
            	json.append(",");
            }
		}
		
		json.append("]}");
		
		return json;
	}
	
	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub
		
	}

}